﻿using System;
using System.Collections.ObjectModel;

namespace Gonte.Wpf.Controls.ViewModel
{
    /// <summary>
    /// Strong typed view model
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class ViewModelTemplate<T>
        : ViewModelBase
        where T : IComparable
    {
        #region Constructors

        public ViewModelTemplate(T model, ViewModelBase parent, bool loadOnDemand)
            : base(parent, loadOnDemand)
        {
            model_ = model;

            if (!IsLoadedOnDemand)
            {
                LoadChildren();
            }

            UpdateIconSource();
        } 

        #endregion

        #region Properties

        //public ReadOnlyCollection<ViewModelTemplate<T>> This
        //{
        //    get
        //    {
        //        return new ReadOnlyCollection<ViewModelTemplate<T>>
        //        (
        //            new ViewModelTemplate<T>[] { this }
        //        );
        //    }
        //}

        public override Object Model
        {
            get
            {
                return model_;
            }

            set
            {
                if (model_ == null
                    || (model_ != null && !model_.Equals(value)))
                {
                    model_ = (T)value;

                    OnPropertyChanged("Model");
                }
            }
        }

        public T ConcreteModel
        {
            get
            {
                return model_;
            }

            set
            {
                if (model_ == null
                    || (model_ != null && !model_.Equals(value)))
                {
                    model_ = value;

                    OnPropertyChanged("Model");
                }
            }
        } 

        #endregion

        #region Overridables

        public override int CompareTo(Object @object)
        {
            ViewModelBase viewModel = (ViewModelBase)@object;
            T model = (T)viewModel.Model;
            return model_.CompareTo(model);
        }

        #endregion

        #region Fields

        private T model_; 

        #endregion
    }
}
